package com.lanou.fooddemo.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.theme.FoodTheme;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by dllo on 2015/7/29.
 */
public class CommonUtil {
    /**
     * 判断list是否非空
     * @param list
     * @return true 非空 false 空
     */
    public static boolean isListNotNull(List<? extends Object> list) {
        return (null != list && 0 < list.size());
    }

    /**
     * 将中文编码字符串转换成UTF-8
     * @param city 中文字符串
     * @return UTF-8编码字符串
     */
    public static String getUTF8OfString(String city) {
        try {
            return URLEncoder.encode(city, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置控件点击抖动
     * @param c 控件所在Context
     * @param v 控件
     */
    public static void setAnimation(Context c, View v) {
        Animation animation = AnimationUtils.loadAnimation(c, R.anim.image_translate);
        animation.start();
        v.setAnimation(animation);
    }

    /**
     * 设置list长按动画
     * @param c
     * @param v
     */
    public static void setListItemLongClickAnimation(Context c, View v) {
        Animation animation = AnimationUtils.loadAnimation(c, R.anim.list_long_click);
        animation.start();
        v.setAnimation(animation);
    }
    private static void setFoodTheme(Activity activity, String theme) {
        SharedPreferences sp = activity.getSharedPreferences(FoodTheme.NAME, activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(FoodTheme.NAME, theme);
        editor.commit();

        FoodTheme.setTheme(theme);
    }

    /**
     * 设置红色主题接口
     * @param activity
     */
    public static void setFoodThemeRed(Activity activity) {
        setFoodTheme(activity, FoodTheme.TYPE_RED);
    }

    /**
     * 设置蓝色主题接口
     * @param activity
     */
    public static void setFoodThemeBlue(Activity activity) {
        setFoodTheme(activity, FoodTheme.TYPE_BLUE);
    }

    /**
     * 从存储获取主题接口
     * @param activity
     * @return
     */
    public static void getFoodThemeOfSharedPerferences(Activity activity) {
        SharedPreferences sp = activity.getSharedPreferences(FoodTheme.NAME, activity.MODE_PRIVATE);
        FoodTheme.setTheme(sp.getString(FoodTheme.NAME, FoodTheme.TYPE_RED));
    }

}
